/*
根据菜单id返回路径['menu01','menu0101','menu010101']
*/
export const findNodePath = function (tree, targetValue, path = []) {
  for (let node of tree) {
    if (node.value === targetValue) {
      return [...path, node.value]
    }
    if (node.children && node.children.length > 0) {
      let result = findNodePath(node.children, targetValue, [...path, node.value])
      if (result) {
        return result
      }
    }
  }
  return null // 如果没有找到，则返回null
}

/*
树扁平化
*/
export const treeToFlat = function(tree, parentId = null) {
  let flat = []
  for (let node of tree) {
    let copy = { ...node, parentId }
    flat.push(copy)
    if (node.children && node.children.length > 0) {
      flat = flat.concat(treeToFlat(node.children, node.id))
    }
  }
  return flat
}
